package com.ssy.lingxi.member.merchant.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.ssy.lingxi.member.merchant.handler.converter.JpaJsonToAuthBOConverter;
import com.ssy.lingxi.member.merchant.model.bo.AuthBO;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 会员自定义的角色（用户角色）
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-28
 */
@Entity
@Table(schema = "public", name = "ms_mc_member_role", indexes = {@Index(name = "ms_mc_member_role_member_id_idx", columnList = "member_id")})
public class MemberRoleDO implements Serializable {
    private static final long serialVersionUID = 8140715434234186201L;

    /**
     * 会员角色Id
     */
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="member_role_seq")
    @SequenceGenerator(sequenceName="ms_mc_member_role_seq", name="member_role_seq",allocationSize = 1)
    private Long id;

    /**
     * 会员自定义角色名称
     */
    @Column(columnDefinition = "varchar(20)", nullable = false)
    private String roleName;

    /**
     * 会员自定义角色说明
     */
    @Column(columnDefinition = "varchar(120)")
    private String remark;

    /**
     * 会员自定义角色类型枚举，定义在 MemberUserAuthTypeEnum 中
     */
    @Column(columnDefinition = "int2")
    private Integer typeEnum;

    /**
     * 会员自定义角色状态
     */
    @Column(columnDefinition = "int2")
    private Integer status;

    /**
     * 会员自定义角色权限
     */
    @Convert(converter = JpaJsonToAuthBOConverter.class)
    @Column(columnDefinition = "jsonb")
    private List<AuthBO> auth;

    /**
     * 多对一双向关联会员
     */
    @JsonBackReference
    @ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id", referencedColumnName = "id")
    private MemberDO member;

    /**
     * 多对多双向关联会员下属用户
     */
    @JsonBackReference
    @ManyToMany(mappedBy = "roles", cascade = CascadeType.DETACH, fetch=FetchType.LAZY)
    private Set<MemberUserDO> users;

    /**
     * 是否具有IM即时通信权限
     * */
    @Column
    private Integer hasImAuth;

    /**
     * 一对多单向关联数据权限配置
     */
    @OneToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
    @JoinColumn(name = "member_role_id", referencedColumnName = "id")
    private Set<MemberRoleAuthConfigDO> authConfig;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Integer getTypeEnum() {
        return typeEnum;
    }

    public void setTypeEnum(Integer typeEnum) {
        this.typeEnum = typeEnum;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public List<AuthBO> getAuth() {
        return auth;
    }

    public void setAuth(List<AuthBO> auth) {
        this.auth = auth;
    }

    public MemberDO getMember() {
        return member;
    }

    public void setMember(MemberDO member) {
        this.member = member;
    }

    public Set<MemberUserDO> getUsers() {
        return users;
    }

    public void setUsers(Set<MemberUserDO> users) {
        this.users = users;
    }

    public Integer getHasImAuth() {
        return hasImAuth;
    }

    public void setHasImAuth(Integer hasImAuth) {
        this.hasImAuth = hasImAuth;
    }

    public Set<MemberRoleAuthConfigDO> getAuthConfig() {
        return authConfig;
    }

    public void setAuthConfig(Set<MemberRoleAuthConfigDO> authConfig) {
        this.authConfig = authConfig;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        MemberRoleDO that = (MemberRoleDO) o;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
